package com.example.smartall.ui;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.smartall.R;
import com.example.smartall.api.Api;
import com.example.smartall.api.ApiConfig;
import com.example.smartall.api.NetCallBack;
import com.example.smartall.entity.HospitalBannerBean;
import com.example.smartall.entity.HospitalDetailsBean;
import com.example.smartall.ui.base.BaseActivity;
import com.google.gson.Gson;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.util.ArrayList;
import java.util.List;

/**
 * 医院详情
 */
public class HospitalDetailsActivity extends BaseActivity {

    private int id;

    private ImageView imBack;
    private TextView tvTitle;
    private Banner banner;
    private TextView brief;
    private Button btPervasive;

    private MHandler handler;
    private HospitalDetailsBean.DataBean bean;
    private HospitalBannerBean bannerBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hospital_detials);

        id = getIntent().getIntExtra("hospitalId", -1);
        handler = new MHandler();

        init();
        getDetails();
        getBannerUrls();
        initEvent();
    }

    @Override
    public void init() {
        imBack = (ImageView) findViewById(R.id.im_back);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        banner = (Banner) findViewById(R.id.banner);
        brief = (TextView) findViewById(R.id.brief);
        btPervasive = (Button) findViewById(R.id.bt_pervasive);
        tvTitle.setText("医院详情");
    }

    @Override
    public void initEvent() {
        imBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                HospitalDetailsActivity.this.finish();
            }
        });

        btPervasive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(HospitalDetailsActivity.this,HospitalPatientCardActivity.class));
            }
        });
    }

    private void getDetails() {
        if (bean != null) return;
        Api.config(ApiConfig.HOSPITAL_DETAILS, null).getRequest(new NetCallBack() {
            @Override
            public void onSuccess(String res) {
                HospitalDetailsBean detailsBean = new Gson().fromJson(res, HospitalDetailsBean.class);
                if (detailsBean != null && detailsBean.getCode() == 200) {
                    bean = detailsBean.getData();
                    handler.sendEmptyMessage(0x01);
                }
            }

            @Override
            public void onFailure(Exception e) {

            }
        }, id + "");
    }

    private void getBannerUrls() {
        if (bannerBean != null) return;
        Api.config(ApiConfig.HOSPITAL_BANNER, null).getRequest(new NetCallBack() {
            @Override
            public void onSuccess(String res) {
                bannerBean = new Gson().fromJson(res, HospitalBannerBean.class);
                if (bannerBean.getCode() == 200) {
                    handler.sendEmptyMessage(0x02);
                }
            }

            @Override
            public void onFailure(Exception e) {

            }
        }, "?hospitalId=" + id);
    }

    class MHandler extends Handler {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0x01:
                    brief.setText(bean.getBrief().substring(3,bean.getBrief().length()-4));
                    break;
                case 0x02:
                    List<String> urls = new ArrayList<>();
                    for (HospitalBannerBean.DataBean bean : bannerBean.getData()) {
                        urls.add(ApiConfig.BASE_URL + bean.getImgUrl());
                    }
                    banner.setImages(urls);
                    banner.setImageLoader(new ImageLoader() {
                        @Override
                        public void displayImage(Context context, Object o, ImageView imageView) {
                            Glide.with(context).load(o).into(imageView);
                        }
                    });
                    banner.setDelayTime(2000);
                    banner.start();
                    banner.startAutoPlay();
                    break;
            }
        }
    }
}